package com.aaa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Processor extends Thread{

	private Socket socket;
	private InputStream inputStream;
	private PrintStream outputStream;
	public static final String WEB_ROOT="d:\\aaaserver\\webroot\\";
	public Processor(Socket socket){
		this.socket = socket;
		try {
			inputStream = this.socket.getInputStream();
			outputStream =new PrintStream( this.socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void run(){
		String filename  = parseStream(inputStream);
		sendFile(filename);
	}
	//解析请求资源
	public String parseStream(InputStream in){
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String filename = null;
		try {
			String httpMessage= br.readLine();
			String[]  content= httpMessage.split(" ");
			if (content.length !=3) {
				sendErrorMessage(400, "客户请求错误");
				return null;
			}
			System.out.println("code:"+content[0]+",filename:"+content[1]+",http version:"+content[2]);
			filename = content[1];
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filename;
	}
	//请求的资源不存在
	public void sendErrorMessage(int errorCode,String errorMessage){
		outputStream.println("HTTP/1.0 " + errorCode+" "+errorMessage);
		outputStream.println("content-type: text/html");
		outputStream.println();
		outputStream.println("<html>");
		outputStream.println("<head>");
		outputStream.println("<title>Error Message</title>");
		outputStream.println("</head>");
		outputStream.println("<body>");
		outputStream.println("<h1>ErrorCode:" + errorCode +", 错误信息：" +errorMessage+"</h1>");
		outputStream.println("<body>");
		outputStream.println("</body>");
		outputStream.println("</html>");
		outputStream.flush();
		outputStream.close();
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//返回访问的文件
	public void sendFile(String filename) {
		File file = new File(Processor.WEB_ROOT+filename); //找到文件
		if (!file.exists()) {
			sendErrorMessage(404, "File Not Found,你请求的资源不存在");
			return;
		}
		try {
			//找到文件
			InputStream inputStream = new FileInputStream(file);
			byte content[] = new byte[(int)file.length()];//字节数组的大小就是文件的大小
			inputStream.read(content);
			String header="HTTP/1.0 200 OK\r\n"+  "Server: AAA Server 1.0\r\n"+  
            "Content-length: "+content.length+"\r\n"+  "Content-type: text/html\r\n\r\n";  
			outputStream.println();
			outputStream.write(header.getBytes("ASCII"));
			outputStream.write(content);
			outputStream.flush();
			outputStream.close();
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
